<template>
  <el-dialog
    :title="!dataForm.gsmBargainId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="psmId">
      <el-input v-model="dataForm.psmId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="活动名称" prop="gsmBargainName">
      <el-input v-model="dataForm.gsmBargainName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="活动开始时间" prop="gsmBargainStartTime">
      <el-input v-model="dataForm.gsmBargainStartTime" placeholder="活动开始时间"></el-input>
    </el-form-item>
    <el-form-item label="活动结束时间" prop="gsmBargainEndTime">
      <el-input v-model="dataForm.gsmBargainEndTime" placeholder="活动结束时间"></el-input>
    </el-form-item>
    <el-form-item label="每人限购数量" prop="gsmBargainLimitCount">
      <el-input v-model="dataForm.gsmBargainLimitCount" placeholder="每人限购数量"></el-input>
    </el-form-item>
    <el-form-item label="商品底价" prop="psmBargainPrice">
      <el-input v-model="dataForm.psmBargainPrice" placeholder="商品底价"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="gsmBargainStatus">
      <el-input v-model="dataForm.gsmBargainStatus" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="当前参与砍价人数" prop="gsmBargainJoinCount">
      <el-input v-model="dataForm.gsmBargainJoinCount" placeholder="当前参与砍价人数"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则(共砍多少刀)" prop="gsmBargainMaxCount">
      <el-input v-model="dataForm.gsmBargainMaxCount" placeholder="砍价规则(共砍多少刀)"></el-input>
    </el-form-item>
    <el-form-item label="砍价规则(最多砍多少钱)" prop="gsmBargainMaxPrice">
      <el-input v-model="dataForm.gsmBargainMaxPrice" placeholder="砍价规则(最多砍多少钱)"></el-input>
    </el-form-item>
    <el-form-item label="活动描述" prop="gsmBargainShare">
      <el-input v-model="dataForm.gsmBargainShare" placeholder="活动描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gsmBargainId: 0,
          psmId: '',
          gsmBargainName: '',
          gsmBargainStartTime: '',
          gsmBargainEndTime: '',
          gsmBargainLimitCount: '',
          psmBargainPrice: '',
          gsmBargainStatus: '',
          gsmBargainJoinCount: '',
          gsmBargainMaxCount: '',
          gsmBargainMaxPrice: '',
          gsmBargainShare: ''
        },
        dataRule: {
          psmId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          gsmBargainName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          gsmBargainStartTime: [
            { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
          ],
          gsmBargainEndTime: [
            { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
          ],
          gsmBargainLimitCount: [
            { required: true, message: '每人限购数量不能为空', trigger: 'blur' }
          ],
          psmBargainPrice: [
            { required: true, message: '商品底价不能为空', trigger: 'blur' }
          ],
          gsmBargainStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          gsmBargainJoinCount: [
            { required: true, message: '当前参与砍价人数不能为空', trigger: 'blur' }
          ],
          gsmBargainMaxCount: [
            { required: true, message: '砍价规则(共砍多少刀)不能为空', trigger: 'blur' }
          ],
          gsmBargainMaxPrice: [
            { required: true, message: '砍价规则(最多砍多少钱)不能为空', trigger: 'blur' }
          ],
          gsmBargainShare: [
            { required: true, message: '活动描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gsmBargainId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmBargainId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/info/${this.dataForm.gsmBargainId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.psmId = data.bargain.psmId
                this.dataForm.gsmBargainName = data.bargain.gsmBargainName
                this.dataForm.gsmBargainStartTime = data.bargain.gsmBargainStartTime
                this.dataForm.gsmBargainEndTime = data.bargain.gsmBargainEndTime
                this.dataForm.gsmBargainLimitCount = data.bargain.gsmBargainLimitCount
                this.dataForm.psmBargainPrice = data.bargain.psmBargainPrice
                this.dataForm.gsmBargainStatus = data.bargain.gsmBargainStatus
                this.dataForm.gsmBargainJoinCount = data.bargain.gsmBargainJoinCount
                this.dataForm.gsmBargainMaxCount = data.bargain.gsmBargainMaxCount
                this.dataForm.gsmBargainMaxPrice = data.bargain.gsmBargainMaxPrice
                this.dataForm.gsmBargainShare = data.bargain.gsmBargainShare
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/${!this.dataForm.gsmBargainId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmBargainId': this.dataForm.gsmBargainId || undefined,
                'psmId': this.dataForm.psmId,
                'gsmBargainName': this.dataForm.gsmBargainName,
                'gsmBargainStartTime': this.dataForm.gsmBargainStartTime,
                'gsmBargainEndTime': this.dataForm.gsmBargainEndTime,
                'gsmBargainLimitCount': this.dataForm.gsmBargainLimitCount,
                'psmBargainPrice': this.dataForm.psmBargainPrice,
                'gsmBargainStatus': this.dataForm.gsmBargainStatus,
                'gsmBargainJoinCount': this.dataForm.gsmBargainJoinCount,
                'gsmBargainMaxCount': this.dataForm.gsmBargainMaxCount,
                'gsmBargainMaxPrice': this.dataForm.gsmBargainMaxPrice,
                'gsmBargainShare': this.dataForm.gsmBargainShare
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
